<template>
    <div>
        <Card>
            <Tree :data="buildOrganTree"></Tree>
        </Card>
        <Modal v-model="showAdd" width="360" @on-cancel="cancelModel">
            <p slot="header" style="color:#f60;text-align:center">
                <Icon type="information-circled"></Icon>
                <span>请填写组织机构信息</span>
            </p>
            <div>
                 <Form :model="formItem" label-position="top">
                    <FormItem label="组织机构代码：">
                        <Input v-model="formItem.organCode" placeholder="可用英文首字母大写">
                        </Input>
                    </FormItem>
                    <FormItem label="组织机构名称：">
                        <Input v-model="formItem.organName" placeholder="请填写内容">
                        </Input>
                    </FormItem>
                </Form>
            </div>
            <div slot="footer">
                <Button type="info" size="large" long @click="save">保存</Button>
            </div>
        </Modal>
    </div>
</template>
<script>
    export default {
        data () {
            return {
                baseURL: '/user/organ',
                showAdd: false,
                organs: [],
                buttonProps: {
                    type: 'ghost',
                    size: 'small',
                },
                formItem: {
                    organCode: '',
                    organName: '',
                    parentId: '',
                    level: ''
                }
            }
        },
        computed: {
            buildOrganTree: function () {
                let organList = []
                this.organs.forEach((organ) => {
                    let treeNode = {
                        id: organ.id,
                        title: organ.organName,
                        level: organ.level,
                        expand: true,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    fontWeight: 'bold',
                                    fontSize: '15px',
                                    color: 'red',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h('Icon', {
                                        props: {
                                            type: 'location'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ]),
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        float: 'right',
                                        marginRight: '1200px'
                                    }
                                }, [
                                    h('Button', {
                                        props: Object.assign({}, this.buttonProps, {
                                            icon: 'ios-plus-empty',
                                            type: 'primary'
                                        }),
                                        style: {
                                            width: '94px'
                                        },
                                        on: {
                                            click: () => { this.append(data) }
                                        }
                                    })
                                ])
                            ]);
                        }
                    }
                    treeNode.children = []
                    if (organ.children) {
                        this.getChildByOrgan(organ.children, treeNode.children)
                    }
                    organList.push(treeNode)
                }, this);
                return organList
            }
        },
        methods: {
            getChildByOrgan (organs, children) {
                organs.forEach((organ) => {
                    let colorer = '#20B2AA'
                    if (organ.level === 1) {
                        colorer = '#FFD700'
                    } else if (organ.level === 2) {
                        colorer = '#9932CC'
                    } else if (organ.level === 3) {
                        colorer = '#1E90FF'
                    }
                    let child = {
                        id: organ.id,
                        title: organ.organName,
                        level: organ.level,
                        expand: true,
                        render: (h, { root, node, data }) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    fontWeight: 'bold',
                                    fontSize: '15px',
                                    color: colorer,
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h('Icon', {
                                        props: {
                                            type: 'location'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ]),
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        float: 'right',
                                        marginRight: '1200px'
                                    }
                                }, [
                                    h('Button', {
                                        props: Object.assign({}, this.buttonProps, {
                                            icon: 'ios-plus-empty',
                                            type: 'primary',
                                            size: 'default'
                                        }),
                                        style: {
                                            marginRight: '4px',
                                            width: '45px'
                                        },
                                        on: {
                                            click: () => { this.append(data) }
                                        }
                                    }),
                                    h('Button', {
                                        props: Object.assign({}, this.buttonProps, {
                                            icon: 'ios-minus-empty',
                                            type: 'primary',
                                            size: 'default'
                                        }),
                                        style: {
                                            width: '45px'
                                        },
                                        on: {
                                            click: () => { this.remove(root, node, data) }
                                        }
                                    })
                                ])
                            ]);
                        }
                    }
                    child.children = []
                    if (organ.children && organ.children.length > 0) {
                        this.getChildByOrgan(organ.children, child.children)
                    }
                    children.push(child)
                }, this);
            },
            cancelModel () {
                this.$bus.init(this.formItem)
            },
            append (data) {
                this.formItem.parentId = data.id
                this.formItem.level = parseInt(data.level) + 1
                this.showAdd = true
            },
            remove (root, node, data) {
                this.$bus.modal('info', '系统提示', '功能建设中...')
            },
            save () {
                let url = this.baseURL + '/saveOrUpdate'
                this.$ajax.post(url, this.formItem).then((response) => {
                    let {code} = response.data
                    if (code === 0) {
                        this.init();
                        this.showAdd = false
                        this.$bus.alert("success", "保存成功", 3)
                    }
                }, this)
            },
            init () {
                let url = this.baseURL + '/findAll'
                this.$ajax.get(url).then((response) => {
                    let {code, data} = response.data
                    if (code === 0) {
                        this.organs = data
                    }
                }, this).then((error) => {
                    if (error) {
                        this.$bus.error()
                    }
                }, this)
            }
        },
        mounted () {
            this.init()
        }
    }
</script>